#### My (demuredemeanor) sub source ssh-agent script
# Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
# https://notabug.org/demure/dotfiles/
# legacy repo http://github.com/demure/dotfiles
# vim:set syntax=sh:

## Add check for key exists first
## Add check -> Y/N for ssh_env existing? might not need, if using __ssh_agent right
## Re-add root/sudo check now that logname is dead?

## Note: logname is dead https://unix.stackexchange.com/questions/347889/a-proper-replacement-for-the-logname-utility

## SSH Agent
function __ssh_agent() {
    local SSH_ENV="${HOME}/.ssh/environment"

    function __start_agent {
        echo "Initialising new SSH agent..."
        /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
        echo succeeded
        chmod 600 "${SSH_ENV}"
        . "${SSH_ENV}" > /dev/null
        /usr/bin/ssh-add;
    }

    ## Source SSH settings, if applicable
    if [ -f "${SSH_ENV}" ]; then
        . "${SSH_ENV}" > /dev/null
        ps -ef 2>/dev/null | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
            pstree -up ${USER} 2>/dev/null | grep ${SSH_AGENT_PID} | grep ^ssh-agent > /dev/null || {
                __start_agent;
            }
        }
        else
        __start_agent;
    fi
}
